//
//  MYRegisterSetPasswordVC.m
//  MuYing
//
//  Created by tbb on 2019/7/22.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYRegisterSetPasswordVC.h"
#import "MYRegisterSetUserNameVC.h"
@interface MYRegisterSetPasswordVC ()<UITextFieldDelegate>

@end

@implementation MYRegisterSetPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)setupView {
    [super setupView];
    [self.listTableView registerNib:[UINib nibWithNibName:@"MYAccountsCommonHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MYAccountsCommonHeaderView"];
    [self.listTableView registerClass:[MYRegisterSetPasswordTextFieldCell class] forCellReuseIdentifier:@"MYRegisterSetPasswordTextFieldCell"];
    [self.listTableView registerClass:[MYLoginBaseBtnCell class] forCellReuseIdentifier:@"MYLoginBaseBtnCell"];
    [self.listTableView registerNib:[UINib nibWithNibName:@"MYRegisterSetPasswordRemindCell" bundle:nil] forCellReuseIdentifier:@"MYRegisterSetPasswordRemindCell"];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.row == 0 || indexPath.row == 1) {
        MYRegisterSetPasswordTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYRegisterSetPasswordTextFieldCell"];
        if (indexPath.row == 0) {
            cell.placeholderStr = kYQLS(@"请输入密码");
        }else{
            cell.placeholderStr = kYQLS(@"请再输入一次密码");
        }
        cell.textField.delegate = self;
        return cell;
    }else if (indexPath.row == 2) {
        MYRegisterSetPasswordRemindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYRegisterSetPasswordRemindCell"];
        return cell;
    }else{
        MYLoginBaseBtnCell *nextCell = [tableView dequeueReusableCellWithIdentifier:@"MYLoginBaseBtnCell"];
        [nextCell.loginBtn setBackgroundColor:HexColor(0x79C4C7)];
        [nextCell.loginBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
        [nextCell.loginBtn addTarget:self action:@selector(nextAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [nextCell.loginBtn setTitle:kYQLS(@"下一步") forState:(UIControlStateNormal)];
        
        return nextCell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath == 0 || indexPath.row == 1) {
        return 56;
    }else if (indexPath.row == 2) {
        if ([kYQLS(@"下一步") isEqualToString:@"Next step"]) {
            return 50;
        }
        return 25;
    }else {
        return 100;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MYAccountsCommonHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYAccountsCommonHeaderView"];
    headerView.titleLb.text = kYQLS(@"设置密码");
    headerView.subheadLb.text = kYQLS(@"请设置6-12位密码");
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 115;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

-(void)nextAction:(UIButton *)sender {
    
    MYRegisterSetPasswordTextFieldCell *oneCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    MYRegisterSetPasswordTextFieldCell *againCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];

    if (oneCell.textField.text.length <6){
        [MBProgressHUD showSuccess:kYQLS(@"请输入至少6位密码")];
        return;
    }
    
    if (againCell.textField.text.length <6){
        [MBProgressHUD showSuccess:kYQLS(@"请输入至少6位密码")];
        return;
    }
    
    [self updateUserPasswordApi:oneCell.textField.text];
    
}

#pragma mark - 设置用户密码
- (void)updateUserPasswordApi:(NSString *)pass
{
    NSMutableDictionary *dic = NSMutableDictionary.new;
    NSString *url = @"";
    if (self.emailDict) {
        url = kPOST_EMAIL_REGIEST_URL;
        [dic addEntriesFromDictionary:self.emailDict];
        dic[@"password"] = pass;
    }else{
        url = kPOST_SAVE_PASSWORD_URL;
        dic[@"userId"] = self.userId;
        dic[@"password"] = pass;
    }

    [YQPNetWorkingManager sendPostRequest:url parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [MBProgressHUD showSuccess: responseObject.msg];
        if (responseObject.code == 0) {
            if (self.isBack) {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
                MYRegisterSetUserNameVC *nameVC = [[MYRegisterSetUserNameVC alloc] init];
                NSString *userID = [NSString stringWithFormat:@"%@",responseObject.data];
                nameVC.userId = self.userId?self.userId:userID;
                kYQPUSH(nameVC);
            }
        }

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [MBProgressHUD showSuccess: error];
    }];
}

#pragma mark - uitextfield delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    MYRegisterSetPasswordTextFieldCell *oneCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    MYRegisterSetPasswordTextFieldCell *againCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    if (oneCell.textField.text.length <6){
        [MBProgressHUD showSuccess:kYQLS(@"请输入至少6位密码")];
    }
    
    if (againCell.textField.text.length <6){
        [MBProgressHUD showSuccess:kYQLS(@"请输入至少6位密码")];
    }
    
    if (oneCell.textField.text.length && againCell.textField.text.length && ![againCell.textField.text isEqualToString:oneCell.textField.text]) {
        [MBProgressHUD showSuccess:kYQLS(@"两次输入的密码不一致")];
        
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    MYRegisterSetPasswordTextFieldCell *oneCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    MYRegisterSetPasswordTextFieldCell *againCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    
    if (oneCell.textField.text.length <6 && textField == oneCell.textField){
        [MBProgressHUD showSuccess:kYQLS(@"请输入至少6位密码")];
        return;
    }
    
    if (againCell.textField.text.length <6 && textField == againCell.textField){
        [MBProgressHUD showSuccess:kYQLS(@"请输入至少6位密码")];
        return;
    }
    
    if (oneCell.textField.text.length && againCell.textField.text.length && ![againCell.textField.text isEqualToString:oneCell.textField.text]) {
        [MBProgressHUD showSuccess:kYQLS(@"两次输入的密码不一致")];
        
    }
}
@end

